package com.test.websocket.controller;

import com.test.websocket.bean.Greeting;
import com.test.websocket.bean.HelloMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author chenxubing
 * @version $Id: TestController.java, v 0.1 2018-09-12 上午9:30 chenxubing Exp $$
 */
@RestController
public class GreetingController {
    @Value("${server.port}")
    private int port;

    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public Greeting greeting(HelloMessage message) throws Exception {
        String host = "";
        return new Greeting("Hello:" + port + HtmlUtils.htmlEscape(host + ":" + message.getName()) + "!");
    }

    @GetMapping("/ip")
    public String ip(HttpServletRequest request){
        return request.getRemoteHost() + "--" + request.getLocalAddr() + ":" + request.getLocalPort();
    }
}
